//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    partial class WorksheetTabResources
    {
        public WorksheetTabResources()
        {
            InitializeComponent();
        }
        void OnSheetTabButtonPreviewMouseDown(object sender, MouseButtonEventArgs args)
        {
            Button b = (Button)sender;
            //Sheet sheet = (Sheet)b.Content;
            Selector itemsControl = WindowServices.GetAncestor<Selector>(sender);
            itemsControl.SelectedItem = b.Content;
        }

        void OnTabTextKeyDown(object sender, KeyEventArgs args)
        {
            TextBox textBox2 = (TextBox)sender;
            ContentPresenter cp = (ContentPresenter)textBox2.TemplatedParent; 
            TextBlock textBlock2 = (TextBlock)cp.ContentTemplate.FindName("textBlock", cp);
            switch (args.Key)
            {
                case Key.Enter:
                    textBox2.Visibility = Visibility.Hidden;
                    textBlock2.Visibility = Visibility.Visible;
                    break;
                case Key.Escape:
                    textBox2.Text = textBlock2.Text;
                    textBlock2.Visibility = Visibility.Visible;
                    textBox2.Visibility = Visibility.Hidden;
                    break;

            }
        }
        void OnTabTextLostFocus(object sender, RoutedEventArgs args)
        {
            TextBox textBox2 = (TextBox)sender;
            ContentPresenter cp = (ContentPresenter)textBox2.TemplatedParent;
            TextBlock textBlock2 = (TextBlock)cp.ContentTemplate.FindName("textBlock", cp);
            textBlock2.Visibility = Visibility.Visible;
            textBox2.Visibility = Visibility.Hidden;

        }
        void OnTabTextPreviewMouseDown(object sender, MouseButtonEventArgs args)
        {
            if (args.ClickCount <= 1)
                return;
            TextBlock textBlock2 = (TextBlock)sender;
            ContentPresenter cp = (ContentPresenter)textBlock2.TemplatedParent;
            TextBox textBox2 = (TextBox)cp.ContentTemplate.FindName("textBox", cp);
            textBlock2.Visibility = Visibility.Hidden;
            textBox2.Visibility = Visibility.Visible;
            textBox2.SelectAll();
            textBox2.Focus();
            args.Handled = true;
        }
        void OnRenameWorksheet(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            MenuItem mi = (MenuItem)sender;
            //Control parent = (Control)mi.TemplatedParent;
            object o = AdditionalTags.GetInheritedTag(mi);
            Button button = (Button) o; //\\ WindowServices.GetAncestorUsingTemplatedParent<Button>(mi);// (Button)parent.Template.FindName("button", parent);
            ContentPresenter cp = (ContentPresenter)button.Template.FindName("cp", button);
            TextBox textBox2 = (TextBox)cp.ContentTemplate.FindName("textBox", cp);
            TextBlock textBlock2 = (TextBlock)cp.ContentTemplate.FindName("textBlock", cp);
            textBlock2.Visibility = Visibility.Hidden;
            textBox2.Visibility = Visibility.Visible;
            textBox2.SelectAll();
            textBox2.Focus();
        }

        private void OnCanRenameWorksheet(object sender, CanExecuteRoutedEventArgs e)
        {
            MenuItem mi = (MenuItem)sender;

            Workbook workbook = SpreadsheetElement.GetWorkbook(mi);
            if (workbook == null)
                return;
            e.CanExecute = !workbook.AreSheetsProtected;
            e.Handled = true;
        }
    }
}
